package modular02.work02;

import java.util.Scanner;

/**
 * @author 陈洛健
 * @date 2020/7/17 18:09
 * @description
 */
public class Player implements Runnable{


    private ThreadLocal<Integer> winner = new ThreadLocal<Integer>(); //用于传递双方输赢

    private Chessboard chessboard; //棋盘

    private int team;// 1为黑棋 2为白队


    /**
     *    -----落子操作-----
     */
    public void move(){
        while(true){
            chessboard.move(team);
            if(chessboard.getWinner() != null && chessboard.getWinner().get() == 1){
                System.out.println("黑棋胜利");
                System.out.println("对局结束");
                break;
            }else if(chessboard.getWinner() != null && 2 == team){
                System.out.println("白棋胜利");
                System.out.println("对局结束");
                break;
            }
        }
    }


    public boolean joinChessboard(Chessboard chessboard){

        switch (chessboard.joinChessboard()){
            case 1:
                System.out.println("执黑棋" );//todo 可加入棋盘编号 标记玩家正在进行的棋局
                team = 1;
                return true;
            case 2:
                System.out.println("执白棋" );//todo 可加入棋盘编号 标记玩家正在进行的棋局
                team = 2;
                return true;
            default:
                return false;
        }
    }

    public Player(Chessboard chessboard) {
        this.chessboard = chessboard;
        this.joinChessboard(chessboard);
    }

    private Player() {
    }

    public Chessboard getChessboard() {
        return chessboard;
    }

    public void setChessboard(Chessboard chessboard) {
        this.chessboard = chessboard;
    }

    public int getTeam() {
        return team;
    }

    public void setTeam(int team) {
        this.team = team;
    }

    @Override
    public void run() {
            move();
    }
}
